﻿using System;
using System.Windows.Forms;
using DBCodeBuilder.Core.Classes;
using DBCodeBuilder.Core.Enum;

namespace DBCodeBuilder.UI
{
    public partial class Config : Form
    {
        public Config()
        {
            InitializeComponent();
            cmbOrm.Items.AddRange(Enum.GetNames(typeof (Orm)));
            cmbAction.Items.AddRange(Enum.GetNames(typeof (SaveAction)));

            cmbOrm.SelectedIndex = 0;
        }

        #region Events

        private void Configurazione_Load(object sender, EventArgs e)
        {
            cmbOrm.SelectedIndex = (int) SessionInfo.Instance.OrmCodeGeneration;

            if (SessionInfo.Instance.FileSaveMode == SaveMode.SingleFile)
                cmbAction.SelectedIndex = (int) SessionInfo.Instance.SaveAction;

            if (SessionInfo.Instance.FileSaveMode == SaveMode.DistinctFile)
                rdbMultipleFile.Checked = true;
            else
                rdbSingleFile.Checked = true;

            txtSaveingFolder.Text = SessionInfo.Instance.SaveFolder;

            chkGenerateEQComp.Checked = SessionInfo.Instance.MapGenSettings.GenereteDomainEqualityComparer;
            chkGeneratePartial.Checked = SessionInfo.Instance.MapGenSettings.GeneretePartialDomain;
        }

        private void btnSaveConfig_Click(object sender, EventArgs e)
        {
            SessionInfo.Instance.OrmCodeGeneration = (Orm) cmbOrm.SelectedIndex;

            if (rdbSingleFile.Checked)
                SessionInfo.Instance.SaveAction = (SaveAction) cmbAction.SelectedIndex;

            if (rdbMultipleFile.Checked)
                SessionInfo.Instance.FileSaveMode = SaveMode.DistinctFile;

            if (rdbSingleFile.Checked)
                SessionInfo.Instance.FileSaveMode = SaveMode.SingleFile;

            if (!String.IsNullOrEmpty(txtSaveingFolder.Text))
                SessionInfo.Instance.SaveFolder = txtSaveingFolder.Text;

            SessionInfo.Instance.MapGenSettings.GenereteDomainEqualityComparer = chkGenerateEQComp.Checked;
            SessionInfo.Instance.MapGenSettings.GeneretePartialDomain = chkGeneratePartial.Checked;
            SessionInfo.SaveConfig();
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                txtSaveingFolder.Text = folderBrowserDialog1.SelectedPath;
        }

        private void rdbSingleFile_CheckedChanged(object sender, EventArgs e)
        {
            cmbAction.Enabled = rdbSingleFile.Checked;
        }
        #endregion
    }
}